// ==UserScript==
// @name         九宫格轻触翻页（适用于手机浏览器）
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  手机端网页九宫格，中间格不动，其余格子翻页
// @author       Goocheez
// @match        https://www.kuaishu5.com/*/*
// @match        https://m.9cwx.org/*/*
// @match        https://www.mayiwsk.com/*/*
// @grant        none
// ==/UserScript==


(function () {
  'use strict';
//--------------------------------------------------------------------------------------
//                           总引导
//--------------------------------------------------------------------------------------
  const actions = {
    'm.biquge5200.net': ()=>{ common() },
    'www.qidian.com':   ()=>{ common() },
    'www.beqege.cc':    ()=>{ common() },
    'www.22biqu.com':   ()=>{ common() },
    'www.kuaishu5.com': ()=>{ kuaishu5() },
    'www.shuzhaige.com':()=>{ common() },
    'm.9cwx.org':       ()=>{ cwx() },
    'www.mayiwsk.com':  ()=>{ mayiwsk() },
  }

  if ( actions[window.location.hostname] ){
    actions[window.location.hostname]();
  }


  //--------------------------------------------------------------------------------------
  //                           主要函数们
  //--------------------------------------------------------------------------------------

  function createPagedownGrid () {
    const grid = document.createElement('div');
    const id = 'gm-nine-grid';
    const mark_class = 'gm-scroll-mark';
    grid.id = id;
    Object.assign(grid.style, {
      position: 'fixed',
      inset: '0',
      zIndex: '9999',
      display: 'grid',
      gridTemplateColumns: '1fr 1fr 1fr',
      gridTemplateRows: '1fr 1fr 1fr',
      pointerEvents: 'none',   // 让容器本身不拦截事件
      background: 'transparent'
    });

    for (let i = 0; i < 9; i++) {
      const cell = document.createElement('div');
      cell.dataset.idx = i;

      const needHandle = i < 6; // 前6方块
      Object.assign(cell.style, {
        pointerEvents: needHandle ? 'auto' : 'none',
        background: 'transparent'
      });

      // 只对前 6 格绑定翻页动作
      if (needHandle) {
        //  touchend + 短按判定，避免误触
        let startX, startY, startT;
        cell.addEventListener('touchstart', e => {
            startX = e.touches[0].clientX;
            startY = e.touches[0].clientY;
            startT = Date.now();
        });
        cell.addEventListener('touchend', e => {
            const dx = e.changedTouches[0].clientX - startX;
            const dy = e.changedTouches[0].clientY - startY;
            const dt = Date.now() - startT;
            if (dt < 200 && Math.hypot(dx, dy) < 10) {
                e.preventDefault();
                const markY = window.scrollY + window.innerHeight; //翻页前的底部
                window.scrollBy({ top: window.innerHeight * 0.9, behavior: 'auto' });

                document.querySelectorAll('.' + mark_class).forEach(l=> l.remove()); // 删除旧线
                
                const mark = document.createElement('div');
                mark.className = mark_class;
                Object.assign(mark.style, {
                    position: 'absolute',
                    left: 0,
                    width: '100%',
                    height: '2px',
                    //background: '#969c65f2',
                    backgroundImage: 'repeating-linear-gradient(to right, #969c65f2 0 8px, transparent 8px 16px)',
                    zIndex: '9998',          // 比九宫格略低即可
                    pointerEvents: 'none',
                    top: markY + 'px'
                });
                document.body.appendChild(mark);
            }
        });
      }
      grid.appendChild(cell);
    }

    document.body.appendChild(grid);
  }


  //--------------------------------------------------------------------------------------
  //                           各网站具体处理
  //--------------------------------------------------------------------------------------

  function common(){
    createPagedownGrid();
  }
  function kuaishu5(){
    createPagedownGrid();
  }

  function cwx(){
    createPagedownGrid();
  }

  function mayiwsk(){
    createPagedownGrid();
    document.querySelectorAll('.apprecom').forEach(l=> l.remove());
  }

})();